package com.music.service.music.impl;

import com.music.dto.music.SongDTO;
import com.music.dto.utils.PagingDTO;
import com.music.dto.utils.TableReqDTO;
import com.music.dto.utils.TableRspDTO;
import com.music.entity.music.SongEntity;
import com.music.mapper.music.SongMapper;
import com.music.service.music.SongService;
import com.music.utils.file.MinioUtil;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SongServiceImpl implements SongService {
    @Resource
    private SongMapper songMapper;
    private final MinioUtil minioUtil = MinioUtil.getInstance();

    private final Logger logger = LoggerFactory.getLogger(PlaylistServiceImpl.class);

    @Override
    public TableRspDTO queryByPlaylistId(TableReqDTO tableReqDTO) throws Exception {
        Long count = songMapper.countByPlaylistId(
                Long.parseLong(tableReqDTO.parseQueryParam("playlistId")),
                tableReqDTO.parseQueryParam("songName"),
                tableReqDTO.parseQueryParam("singerName"),
                tableReqDTO.parseQueryParam("albumName"),
                tableReqDTO.parseQueryParam("isPublic")
        );
        List<SongDTO> listSong = songMapper.queryByPlaylistId(
                tableReqDTO.getStart(),
                tableReqDTO.getPageSize(),
                Long.parseLong(tableReqDTO.parseQueryParam("playlistId")),
                tableReqDTO.parseQueryParam("songName"),
                tableReqDTO.parseQueryParam("singerName"),
                tableReqDTO.parseQueryParam("albumName"),
                tableReqDTO.parseQueryParam("isPublic"),
                tableReqDTO.getSortField(),
                tableReqDTO.getSortOrder());
        for (int i = 0; i < listSong.size(); i++)
            listSong.get(i).setOrderNumber(1 + i + tableReqDTO.getStart());
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                listSong);
        return new TableRspDTO(pagingDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer add(SongDTO songDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        SongEntity songEntity = mapperFactory.getMapperFacade().map(songDTO, SongEntity.class);
        return songMapper.addSong(songEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(SongDTO songDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        SongEntity songEntity = mapperFactory.getMapperFacade().map(songDTO, SongEntity.class);
        return songMapper.updateSong(songEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String songIds) throws Exception {
        List<SongDTO> listSong = new ArrayList<>(songMapper.getAudioUrl(songIds));
        for (SongDTO songDTO : listSong) {
            songMapper.removeSong(songDTO.getSongId()+"");
            if (songDTO.getAudioUrl() != null)
                minioUtil.remove(songDTO.getAudioUrl(), "music");
        }
        return listSong.size();
    }

    @Override
    public TableRspDTO queryDetailByPlaylistId(TableReqDTO tableReqDTO, Long userId) throws Exception {
        Long count = songMapper.countDetailByPlaylistId(
                userId,
                tableReqDTO.parseQueryParam("playlistId"),
                tableReqDTO.parseQueryParam("songName"),
                tableReqDTO.parseQueryParam("singerName"),
                tableReqDTO.parseQueryParam("albumName")
        );
        List<SongDTO> listSong = songMapper.queryDetailByPlaylistId(
                tableReqDTO.getStart(),
                tableReqDTO.getPageSize(),
                userId,
                tableReqDTO.parseQueryParam("playlistId"),
                tableReqDTO.parseQueryParam("songName"),
                tableReqDTO.parseQueryParam("singerName"),
                tableReqDTO.parseQueryParam("albumName"),
                tableReqDTO.getSortField(),
                tableReqDTO.getSortOrder());
        for (int i = 0; i < listSong.size(); i++)
            listSong.get(i).setOrderNumber(1 + i + tableReqDTO.getStart());
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                listSong);
        return new TableRspDTO(pagingDTO);
    }
}
